#-*-coding:utf-8-*-
import math
# 去掉最值求平均
def score_mean(tmpList):
	tmpList.sort()
	newList = tmpList[1:-1]
	return sum(newList)/len(tmpList)

# 判断list内有无重复元素
# key:遍历每个元素，判断每个元素的个数是否大于1，
# 如果大于1 ，则有重复元素，
# 否则表示没有
def is_duplicated(tmpList):
	for data in tmpList:
		if tmpList.count(data) > 1:
			return True
	return False

# 判断list内有无重复元素进阶版
# key:利用set
def is_duplicated_new(tmpList):
	tmpSet = set(tmpList)
	return len(tmpList) != len(tmpSet)

# 列表反转
def reverseList(tmpList):
	return tmpList[::-1]

def getHead(tmpList):
	return tmpList[0] if tmpList else None

def getTail(tmpList):
	return tmpList[-1] if tmpList else None

# 求和
def getSum(num,type = 0):
	if type:
		return sum(range(num))
	else:
		return reduce(lambda x,y:x+y ,range(num))

def strToDict(str):
	m = map(lambda x : x.split(":"),str.split("|"))
	return {mi[0]:mi[1] for mi in m}



# 两个数组合并成一个dict
def createDict(listA,listB):
	return dict(zip(listA,listB))


# str Byte len
def str_byte_len(str):
	str_bytes = str.encode("utf-8")
	return len(str_bytes)


class Student(object):
	def __init__(self,id,name):
		self._id = id
		self._name = name

	def __str__(self):
		return "name is {}".format(self._name)

	@classmethod
	def print_type_name(cls):
		print "this is class method,class name is {} ,".format(cls.__name__)

	@staticmethod
	def print_something(self):
		print "this is static method ,{}".format(self)


if __name__ == "__main__":

	# list 内元素可以不一致
	testList = [1,"2",True,{"5":5}]
	# del 作用 删除list[idx]
	# del testList[2]
	# pop 删除的是最后一个元素
	data = testList.pop()
	print testList,data

	testDict = {
		0:1,
		1:2,
		2:3,
	}
# 删除dic[key]
	del testDict[0]

	print testDict

# pass的作用占一个空行，放在方法下面可以用来定义一个借口
	pass

	# 5/2 且向下取整
	print 5 // 2
	# 等价于 pow(5,2)
	print 5 ** 2
	print pow(5,2)

	print score_mean([0,1,2,3,4,5,6])

	print is_duplicated([0,0,2,2,5,7,9])
	print is_duplicated_new([8,5,6,7,2,1])
	print reverseList([1,5,9,2,3,6,4,7,8])
	print getHead([])
	print getTail([])
	print getSum(5,1)
	print {}.fromkeys(["a","b","c"],[1,2,3])

	print createDict(["a","b","c"],[1,2,3])
	# 可hash对象才能做dict的key,
	print len(str(hash("Tom"))),len(str(hash("Jerry")))

	# set 没有重复元素的容器，通常用来去除重复元素
	testSet = {1,3,5,7}
	print testSet.issubset({1,3,5,7,9,11})

###############string 对象方法##############

	testStr = "  hello world "

	# 去除前后空格
	print testStr.strip()
	print testStr.replace("l","L")
	# join  split 互为逆操作
	print "".join(["111","_","22"])
	print "11|22|33".split("|")

###################end #######################

	print str_byte_len("12 34 56")
	print strToDict("k1:1|k2:3|k5:8")
	# 四舍五入保留n位小数
	print round(10.0228,3)

	tommy = Student(000,"tommy")
	# id()返回对象的地址
	print id(tommy)
	print tommy
	# classmethod 可以同时被对象和类调用
	print Student.print_type_name()
	print tommy.print_type_name()

	# staticmethod 也可以同时被对象和类调用
	tommy.print_something(tommy)
	Student.print_something(tommy)

	if hasattr(tommy,"score"):
		print " has score"
	else:
		print "not has score"

	print getattr(tommy,"_name")


	# list内所有元素都为真
	print all([1,2,3,True,"",0,{}])
	# list内至少有一个元素为真
	print any([1,0,False,"",{}])
	# 不可改变的set
	fs = frozenset([1,2,3])


